//
//  AgreementViewController.m
//  CS Inventory Manager
//
//  Created by Ian Smith on 3/24/13.
//  Copyright (c) 2013 Transylvania University. All rights reserved.
//

#import "AgreementViewController.h"
#import "SuccessScreenViewController.h"
#import "SignatureCaptureView.h"
#import "Patron.h"

@interface AgreementViewController ()

@end

@implementation AgreementViewController
@synthesize selectedDueDate, selectedPatron, selectedItem, cameFromDVC, sigLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//This controls the "I Agree" button and allows for multiple signatures
-(void)agreeButton:(id)sender
{
    //If not everyone has signed yet, wipe the signature field and go to the next person
    if (self.patronCount < selectedPatron.count) {
        SignatureCaptureView *signature = self.childViewControllers[0];
        [self.signatures addObject:signature.drawImage.image];                                  //Grab the current signature and put it in the array
        signature.drawImage.image = nil;                                                        //Wipe the signature field for the next person
        NSLog(@"Signature stored, now have %d signatures!", self.signatures.count);
        Patron *tempPatron = selectedPatron[self.patronCount];
        //Change the label to indicate who should sign next
        NSMutableString *name = [[NSMutableString alloc] initWithString:tempPatron.lastName];   
        [name appendString:@", "];
        [name appendString:tempPatron.firstName];
        sigLabel.text = name;
        _patronCount++;
    } else {
        //If we are done getting signatures from everyone, then store the last signature and go on to the next screen
        SignatureCaptureView *signature = self.childViewControllers[0];
        [self.signatures addObject:signature.drawImage.image];
        signature.drawImage.image = nil;
        NSLog(@"Signature stored, now have %d signatures!", self.signatures.count);
        [self performSegueWithIdentifier:@"Success" sender:NULL];
    }
}

//Back button handler
-(void)backButton:(id)sender
{
    //Dismiss screen and go back
    [self dismissViewControllerAnimated:YES completion:NULL];
}

//Prepare for the success screen by passing everything on to be uploaded to the database
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Success"]) {
        SuccessScreenViewController *nextController = segue.destinationViewController;
        nextController.selectedPatron = selectedPatron;
        nextController.selectedItem = selectedItem;
        nextController.selectedDueDate = selectedDueDate;
        nextController.cameFromDVC = cameFromDVC;
        nextController.signatures = self.signatures;
        nextController.agreement = self.agreementText.text;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    // Need to init patronCount to zero to make sure the multiple signature capability works
    self.patronCount = 0;
    // Init the label to the first person's name
    Patron *tempPatron = selectedPatron[self.patronCount];
    NSMutableString *name = [[NSMutableString alloc] initWithString:tempPatron.lastName];
    [name appendString:@", "];
    [name appendString:tempPatron.firstName];
    sigLabel.text = name;
    _patronCount++;
    // Init the array of signatures
    self.signatures =[[NSMutableArray alloc] init];
    self.navBar.topItem.title = @"Check Out Wizard";
    self.view.backgroundColor = [UIColor grayColor];
    ///
    /// Here we have to build the giant string that represents the text of the Agreement students will sign
    ///
    NSMutableString *agreement = [[NSMutableString alloc] initWithString:@"STUDENTS:\n"];
    for (int i = 0; i < selectedPatron.count; i++) {
        Patron *tempPatron = selectedPatron[i];
        NSMutableString *name = [[NSMutableString alloc] initWithString:tempPatron.lastName];
        [name appendString:@", "];
        [name appendString:tempPatron.firstName];
        [name appendString:@"\n"];
        [agreement appendString:name];
    }
    [agreement appendFormat:@"ITEM: %@\nMODEL: %@\nSERIAL: %@\nCOST: %.2f\nVALUE: %.2f\n", selectedItem.myType.manufacturerAndName, selectedItem.model, selectedItem.serialNo, selectedItem.cost, selectedItem.itemValue];
    //Parse today's date so that it reads: YYYY-MM-DD
    NSDate *today = [NSDate date];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit| NSMonthCalendarUnit| NSYearCalendarUnit fromDate:today];
    NSMutableString *date = [[NSMutableString alloc] init];
    [date appendFormat:@"%d",components.year];
    [date appendString:@"-"];
    if (components.month < 10) {
        [date appendString:@"0"];
    }
    [date appendFormat:@"%d", components.month];
    [date appendString:@"-"];
    if (components.day < 10) {
        [date appendString:@"0"];
    }
    [date appendFormat:@"%d", components.day];
    [agreement appendFormat:@"CHECKOUT DATE: %@\n", date];
    //Parse the due date so that it reads: YYYY-MM-DD
    components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit| NSMonthCalendarUnit| NSYearCalendarUnit fromDate:selectedDueDate];
    date = [[NSMutableString alloc] init];
    [date appendFormat:@"%d",components.year];
    [date appendString:@"-"];
    if (components.month < 10) {
        [date appendString:@"0"];
    }
    [date appendFormat:@"%d", components.month];
    [date appendString:@"-"];
    if (components.day < 10) {
        [date appendString:@"0"];
    }
    [date appendFormat:@"%d", components.day];
    [agreement appendFormat:@"DUE DATE: %@\n", date];
    self.agreementText.text = agreement;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
